package com.qqtech.qquan.im.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleBaseStatusDictEnum;
import com.qqtech.qquan.circle.enums.CircleFuncTypeDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseQuery;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleMemberQuery;
import com.qqtech.qquan.circle.model.CircleMemberVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.im.enums.ImRetrunEnum;
import com.qqtech.qquan.im.model.ImGroup;
import com.qqtech.qquan.im.model.ImGroupQuery;
import com.qqtech.qquan.im.model.ImGroupVo;
import com.qqtech.qquan.im.service.ImGroupService;
import com.qqtech.qquan.user.enums.UserConst;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/im/group")
public class ImGroupController extends BaseWebController<ImGroup, ImGroupQuery> {
	@Resource
	private ImGroupService imGroupService;

	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleMemberService circleMemberService;
	
	@Override
	protected BaseService<ImGroup> getBaseService() {
		return imGroupService;
	}
	
	/**
	 * 初始化群组到环信
	 * @return
	 */
	@RequestMapping(value = "/initImGroup2HuanXin")
	@ResponseBody
	public Result initImGroup2HuanXin(){		
		Result result = new Result();
		StringBuffer addCircleRet =  new StringBuffer();;
		int addCircleCount = 0;
		int addCircleMemberCount =0;
		CircleBaseQuery circleQ = new CircleBaseQuery();
		circleQ.setStatus(CircleBaseStatusDictEnum.NORMAL.getCode());
		
		int circleCount4Chat = circleBaseService.queryCount(circleQ);
		int pageCount = (circleCount4Chat/30)+1;//总页数
		for(int i=1;i<=pageCount;i++){
			Pageable pageKit = new PageKit(i, 30, new PageOrder(Direction.ASC, "id"));
			List<CircleBaseVo> circleList = circleBaseService.queryList(circleQ,pageKit);
			if(circleList!=null && circleList.size()>0){
				
				for(CircleBaseVo circle : circleList){
					if(circle.getFuncType() != CircleFuncTypeDictEnum.TOPICONLY.getCode()){
						int imGroupId = imGroupService.addImGroup(circle.getId(), circle.getName(), 1000, circle.getCreatorId());
						if(imGroupId>0){
							addCircleCount++;
							addCircleRet.append(circle.getName()+",");
						}
					}
				}
			}
		}
		
		
		CircleMemberQuery circleMemberQ = new CircleMemberQuery();
		int circleMemberCount4Chat = circleMemberService.queryCount(circleMemberQ);
		int pageCount4Member = (circleMemberCount4Chat/30)+1;//总页数
		for(int i=1;i<=pageCount4Member;i++){
			Pageable pageKit2 = new PageKit(i, 30, new PageOrder(Direction.ASC, "id"));
			List<CircleMemberVo> circleMemberList = circleMemberService.queryList(circleMemberQ, pageKit2);
			if(circleMemberList!=null && circleMemberList.size()>0){
				for(CircleMemberVo circleMember : circleMemberList){
					int ret = imGroupService.addGroupMember(circleMember.getCircleId(), circleMember.getMemberId());
					if(ret == ImRetrunEnum.SUCESS.getCode()){
						addCircleMemberCount++;
					}
				}
			}
		}
		
		result.put("addMsg", "本次添加im群组："+addCircleCount+"个：\n"+addCircleRet.toString());
		result.put("addMsg2", "本次添加im群组成员："+addCircleMemberCount+"个");
		return result;
	}
	
	/**
	 * 查询一个IM群组信息
	 * @param imUserQ
	 * @return
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/getImGroup")
	@ResponseBody
	public Result getImGroup(@RequestBody ImGroupQuery  imGroupQ){
		Result result = new Result();
		Integer groupId = imGroupQ.getGroupId();//圈子ID
		String imGroupId = imGroupQ.getImGroupId();//群组ID
		
		CircleBaseVo circleVo = null;
		ImGroupVo imGroup = null;
		
		if(groupId!=null){
			circleVo = circleBaseService.getById(groupId);
			imGroup = imGroupService.getByGroupId(groupId);
		}else if(imGroupId!=null){
			imGroup = imGroupService.getByImGroupId(imGroupId);
			if(imGroup!=null && imGroup.getGroupId()!=null){
				circleVo = circleBaseService.getById(imGroup.getGroupId());
			}
		}else{
			result.addErr("群组信息groupId/imGroupId为空！");
			return result;
		}
		
		if(circleVo!=null && imGroup!=null){
			imGroup.setLogo(circleVo.getLogo());
			imGroup.setName(circleVo.getName());
		}
		if(circleVo!=null){
			imGroup.setIsCircle(circleVo.isGroupChat()? YesNoEnum.NO.getCode() : YesNoEnum.YES.getCode());
			List<CircleMemberVo> memberList = circleMemberService.queryCircleFullMember(circleVo.getId());
			imGroup.setImgString(getGroupChatLogoString(memberList), UserConst.IMG_POSTFIX);
			if(memberList!=null){
				imGroup.setMemberCount(memberList.size());
			}else{
				imGroup.setMemberCount(0);
			}
		}
		result.put("imGroup", imGroup);	
		return result;
	}
	
	private String getGroupChatLogoString(List<CircleMemberVo> members){
		String imgString = "";
		int count = 0;
		if(members!=null && members.size()>0){
			for(CircleMemberVo member : members){
				count++;
				imgString += member.getMemberImg() +";";
				if(count >= 9){
					break;
				}
			}
		}
		return imgString;
	}
}
